package com.dashan.frontend.client.widgets;

import static com.dashan.Constants.MAP_HEIGHT;

import com.dashan.frontend.client.util.GoogleMap;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.SmallZoomControl3D;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.Margins;
import com.gwtext.client.core.Position;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.TabPanelListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;


public class MainPanel extends Composite {
    
    private final Panel root = new Panel();
    private Panel westPanel;
    private TabPanel tabPanel = new TabPanel();
    private Panel publicSearchTabPanel = new Panel("找朋友");
    private Panel myTabPanel = new Panel("我的状态");
    private Panel friendTabPanel = new Panel("我的好友");
    
    private GoogleMap googleMap;
    private PublicSearchPanel publicSearchPanel;
    private MyPanel myPanel;
    private FriendPanel friendPanel;
   
    
    public MainPanel() {
        MapWidget map = new MapWidget();
        googleMap = new GoogleMap(map);
        
        setupLayout();
        initPublicPanel();
        initMyPanel();
        initFriendPanel();
        
        tabPanel.addListener(new TabPanelListenerAdapter() {
            @Override
            public void onTabChange(TabPanel source, Panel tab) {
                googleMap.clearMarker();
                if (tab.getItemId().compareTo(publicSearchTabPanel.getItemId()) == 0) {
                    publicSearchPanel.onShow();
                } else if (tab.getItemId().compareTo(myTabPanel.getItemId()) == 0){
                    myPanel.onShow();
                } else if (tab.getItemId().compareTo(friendTabPanel.getItemId()) == 0) {
                    friendPanel.onShow();
                }
            }
        });
        
        initWidget(root);
        map.checkResizeAndCenter();
    }

    private void initMyPanel() {
        myPanel = new MyPanel(this);
        myTabPanel.add(myPanel);
        tabPanel.add(myTabPanel);
    }
    private void initPublicPanel() {
        publicSearchPanel = new PublicSearchPanel(this);
        publicSearchTabPanel.add(publicSearchPanel);
        tabPanel.add(publicSearchTabPanel);
    }
    private void initFriendPanel() {
        friendPanel = new FriendPanel(this);
        friendTabPanel.add(friendPanel);
        tabPanel.add(friendTabPanel);
    }
    
    private void setupLayout() {
        Panel borderPanel = new Panel();
        root.setLayout(new FitLayout());
        borderPanel.setLayout(new BorderLayout());
        root.add(borderPanel);
        root.setHeight(MAP_HEIGHT);
        googleMap.getMap().setHeight(MAP_HEIGHT + "px");
        googleMap.getMap().addControl(new SmallZoomControl3D());
        
        westPanel = new Panel();
        westPanel.setWidth(300);
        BorderLayoutData westData = new BorderLayoutData(RegionPosition.WEST);
        westData.setSplit(true);
        westData.setMargins(new Margins(0, 5, 0, 0));
        borderPanel.add(westPanel, westData);  
        
        tabPanel.setTabPosition(Position.TOP);  
        tabPanel.setTabWidth(135);  
        tabPanel.setActiveTab(0);
        tabPanel.setHeight(800);
        westPanel.add(tabPanel);
        
        Panel centerPanel = new Panel();
        centerPanel.setHtml("<p>center panel</p>");
        centerPanel.setBodyStyle("background-color:C3D9FF");
        centerPanel.add(googleMap.getMap());
        borderPanel.add(centerPanel, new BorderLayoutData(RegionPosition.CENTER));
    }

    public GoogleMap getGoogleMap() {
        return googleMap;
    }
}
